﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Proggy.Web.Cms.Core.ViewModels;
using Proggy.Web.Cms.Core.Configuration;
using Proggy.Web.Cms.PropertyTypes;
using Newtonsoft.Json;
using Proggy.Web.Cms.Core.Models;
using Proggy.Web.Cms.AppHelpers;
using System.Threading;
using Proggy.Web.Cms.Repositories.Membership;
using Proggy.Web.Cms.Controllers.BackEnd;
using Proggy.Web.Cms.Membership;


namespace Proggy.Web.Cms.Core.Controllers.PropertyType
{
    /// <summary>
    /// ~/ProggyCore/DragAndDrop
    /// </summary>
    [OutputCache(Duration=1, VaryByParam="none")]
    public class DragAndDropController : BackEndControllerBase
    {
        public ActionResult Add(string a, string nodeId)
        {
            return View(new AddDragAndDropPartViewModel { AllowedPartsString = a });
        }

        
        [HttpGet]
        public ActionResult Configure(string part, int nodeId)
        {

            // get part
            var partEl = DragAndDropManager.DragAndDropPartsCache.Single(x => x.Name == part);

            // get instance
            var instance = (PropertyTypeBase)Activator.CreateInstance(partEl.PropertyType);
            instance.PropertyNameStart = "PropertyType";

            // check config
            if (partEl.PropertyType.IsSubclassOf(typeof(ConfigurablePropertyTypeBase)))
            {
                ((ConfigurablePropertyTypeBase)instance).BaseConfigObject = partEl.StaticConfigObject;
            }

            //
            return View(new ConfigureDragAndDropPartViewModel { PropertyType = instance });

        }

        
        [HttpPost]
        public ActionResult Configure(ConfigureDragAndDropPartViewModel model, string part, int nodeId)
        {

            // start error


            if (ModelState.IsValid)
            {
                // create drag and drop item
                var dragAndDropItem = new AddedDragAndDropItem
                {
                    Clear = model.Clear,
                    Float = model.Float,
                    ConfigData = JsonConvert.SerializeObject(model.PropertyType),
                    Margin = model.Margin,
                    PartName = part,
                    Width = model.Width,
                    UniqueId = "dand" + DateTime.Now.ToFileTimeUtc() + "dand",
                    NodeId = nodeId
                };

                // returning view will also make sure it's in the cache
                return View("DragAndDropEditorView", new AddedDragAndDropItemRenderViewModel { Item = dragAndDropItem, RenderAsEditor = true });
            }

            // get part
            var partEl = DragAndDropManager.DragAndDropPartsCache.Single(x => x.Name == part);

            // check config
            if (partEl.PropertyType.IsSubclassOf(typeof(ConfigurablePropertyTypeBase)))
            {
                ((ConfigurablePropertyTypeBase)model.PropertyType).BaseConfigObject = partEl.StaticConfigObject;
            }

            return View(model);
        }

        
        [HttpGet]
        public ActionResult Edit(string tempId, string part, int nodeId)
        {
            // get object
            var addedPart = DragAndDropManager.GetFromAddedPartsCache(tempId);
                //JsonConvert.DeserializeObject<AddedDragAndDropItem>(j);

            // get part
            var partEl = DragAndDropManager.DragAndDropPartsCache.Single(x => x.Name == addedPart.PartName);

            // get instance
            var instance = (PropertyTypeBase)JsonConvert.DeserializeObject(addedPart.ConfigData, partEl.PropertyType);
            instance.PropertyNameStart = "PropertyType";

            // check config
            if (partEl.PropertyType.IsSubclassOf(typeof(ConfigurablePropertyTypeBase)))
            {
                ((ConfigurablePropertyTypeBase)instance).BaseConfigObject = partEl.StaticConfigObject;
            }

            //
            return View("Configure", new ConfigureDragAndDropPartViewModel
            {
                PropertyType = instance,
                Clear = addedPart.Clear,
                Float = addedPart.Float,
                MarginBottomInt = addedPart.GetMarginBottom(),
                MarginLeftInt = addedPart.GetMarginLeft(),
                MarginRightInt = addedPart.GetMarginRight(),
                MarginTopInt = addedPart.GetMarginTop(),
                MarginType = addedPart.GetMarginType(),
                WidthInt = addedPart.GetWidth(),
                WidthType = addedPart.GetWidthType()
            });
        }

        
        [HttpPost]
        public string DeleteFromCache(string tempId) {
            
            // find
            var find = DragAndDropManager.AddedPartsCache.SingleOrDefault(x => x.UniqueId == tempId);

            // check
            if (find != null)
            {
                DragAndDropManager.AddedPartsCache.Remove(find);
                return "Deleted " + tempId;
            }
            else {
                return "Could not find " + tempId;
            }

        }
    }


}
